part of '../index.dart';

class ServiceCard extends StatelessWidget {
  const ServiceCard({
    super.key,
    required this.title,
    required this.avatar,
    this.subTitle,
    this.onTap,
    this.date,
    this.count = 0,
  });
  final String avatar;
  final String title;
  final String? subTitle;
  final Function()? onTap;
  final String? date;
  final int? count;

  Widget _buildSubTitle() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 10),
      child: DefaultTextStyle.merge(
        style: const TextStyle(
          fontWeight: FontWeight.w500,
          fontSize: 12,
          color: Color(0xFFC1C1C1),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: const TextStyle(fontSize: 16, color: Colors.black),
            ),
            lastMessage(),
          ],
        ),
      ),
    );
  }

  FutureBuilder lastMessage() {
    Future<chat.Message?>? last;
    if (title == T.current.c_sys_notice) {
      final conversation = EMConversation.fromJson({
        'convId': ImManager.systemNoticeId,
        'type': 0,
      });
      last = conversation.latestMessage();
    }
    if (title == T.current.c_plt_notice) {
      final conversation = EMConversation.fromJson({
        'convId': ImManager.platformNoticeId,
        'type': 0,
      });
      last = conversation.latestMessage();
    }
    if (title == T.current.c_custom_service) {
      final conversation = EMConversation.fromJson({
        'convId': ImManager.customerId,
        'type': 0,
      });
      last = conversation.latestMessage();
    }
    return FutureBuilder<chat.Message?>(
      future: last,
      builder: (context, snapshot) {
        final show = snapshot.data?.attributes?['title'];
        if (title == T.current.c_custom_service) {
          return Text(ImManager.resolveMessage(snapshot.data) ?? '');
        }
        return Text(show ?? '');
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Slidable(
      child: GestureDetector(
        onTap: onTap,
        behavior: HitTestBehavior.opaque,
        child: Padding(
          padding: const EdgeInsets.only(bottom: 10),
          child: Row(
            children: [
              Image.asset(avatar, width: 56, height: 56),
              _buildSubTitle().expanded,
              Column(
                children: [
                  Text(
                    date ?? '',
                    style:
                        const TextStyle(color: Color(0xFFC1C1C1), fontSize: 12),
                  ),
                  if ((count ?? 0) > 0)
                    Container(
                      width: 20,
                      height: 20,
                      decoration: BoxDecoration(
                        color: const Color(0xFFFF6A6A),
                        borderRadius: BorderRadius.circular(99),
                      ),
                      alignment: Alignment.center,
                      child: Text(
                        '$count',
                        style: const TextStyle(color: Colors.white),
                      ),
                    ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
